package com.fooddiary.dao.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.fooddiary.dao.IRoleDao;
import com.fooddiary.model.Role;
import com.fooddiary.util.AppConstants;

/**
 * Implements DAO methods for roles <br>
 * @author Created by andrey.kuznetsov <br>
 * @author Last modified by $Author$ <br>
 * @author Last modified on $Date$ at revision $Revision$ <br>
 */
@Repository
public class RoleDao extends BaseDao implements IRoleDao{

	private final String PROPERTY_ROLE_NAME = "name";
	
	@SuppressWarnings("unchecked")
	@Override
	public Role getRoleByName(String p_roleName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Role.class)
		.add(Restrictions.eq(PROPERTY_ENABLED, AppConstants.ENTITY_STATE_ENABLED))
		.add(Restrictions.eq(PROPERTY_ROLE_NAME, p_roleName))
		.addOrder(Order.asc(PROPERTY_ID));
		List<Role> roles = hibernateTemplate.findByCriteria(criteria);
		return CollectionUtils.isNotEmpty( roles ) ? roles.get( 0 ) : null;
	}

}
